﻿using System;
using System.Text.RegularExpressions;

namespace Tools.Net
{
    /// <summary>
    /// Network 的摘要说明
    /// </summary>
    public static class Html
    {
        #region 日期随机函数
        /**********************************
         * 函数名称:DateRndName
         * 功能说明:日期随机函数
         * 参    数:ra:随机数
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          Random ra = new Random();
         *          string s = o.DateRndName(ra);
         *          Response.Write(s);
         *          o.Dispose();
         * ********************************/
        /// <summary>
        /// 日期随机函数
        /// </summary>
        /// <param name="ra">随机数</param>
        /// <returns></returns>
        public static string DateRndName(Random ra)
        {
            DateTime d = DateTime.Now;
            string s = null;
            string y = d.Year.ToString();
            string m = d.Month.ToString();
            if (m.Length < 2) m = "0" + m;
            string dd = d.Day.ToString();
            if (dd.Length < 2) dd = "0" + dd;
            string h = d.Hour.ToString();
            if (h.Length < 2) h = "0" + h;
            string mm = d.Minute.ToString();
            if (mm.Length < 2) mm = "0" + mm;
            string ss = d.Second.ToString();
            if (ss.Length < 2) ss = "0" + ss;
            s += y + ',' + m + ',' + dd + ',' + h + "-" + mm + "-" + ss;
            s += ra.Next(1000000, 9999999).ToString();
            return s;
        }
        #endregion

        #region 取得文件后缀
        /**********************************
         * 函数名称:GetFileExtends
         * 功能说明:取得文件后缀
         * 参    数:filename:文件名称
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string url = @"http://www.baidu.com/img/logo.gif";
         *          string s = o.GetFileExtends(url);
         *          Response.Write(s);
         *          o.Dispose();
         * ********************************/
        /// <summary>
        /// 取得文件后缀
        /// </summary>
        /// <param name="filename">文件名称</param>
        /// <returns></returns>
        public static string GetFileExtends(string filename)
        {
            string ext = null;
            if (filename.IndexOf('.') > 0)
            {
                string[] fs = filename.Split('.');
                ext = fs[fs.Length - 1];
            }
            return ext;
        }
        #endregion

        #region 替换网页中的换行和引号
        /**********************************
         * 函数名称:ReplaceEnter
         * 功能说明:替换网页中的换行和引号
         * 参    数:HtmlCode:html源代码
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string Url = @"http://www.baidu.com";
         *          string HtmlCode = o.GetRemoteHtmlCode(Url);
         *          string s = o.ReplaceEnter(HtmlCode);
         *          Response.Write(s);
         *          o.Dispose();
         * ********************************/
        /// <summary>
        /// 替换网页中的换行和引号
        /// </summary>
        /// <param name="htmlCode">HTML源代码</param>
        /// <returns></returns>
        public static string ReplaceEnter(string htmlCode)
        {
            string s = string.IsNullOrEmpty(htmlCode) ? "" : htmlCode.Replace("\"", "");
            s = s.Replace("\r", "");
            s = s.Replace("\n", "");
            return s;
        }

        #endregion

        #region 执行正则提取出值
        /**********************************
         * 函数名称:GetRegValue
         * 功能说明:执行正则提取出值
         * 参    数:HtmlCode:html源代码
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string Url = @"http://www.baidu.com";
         *          string HtmlCode = o.GetRemoteHtmlCode(Url);
         *          string s = o.ReplaceEnter(HtmlCode);
         *          string Reg="<title>.+?</title>";
         *          string GetValue=o.GetRegValue(Reg,HtmlCode)
         *          Response.Write(GetValue);
         *          o.Dispose();
         * ********************************/
        /// <summary>
        /// 执行正则提取出值
        /// </summary>
        /// <param name="regexString">正则表达式</param>
        /// <param name="remoteStr">HtmlCode源代码</param>
        /// <returns></returns>
        public static string GetRegValue(string regexString, string remoteStr)
        {
            string matchVale = "";
            var r = new Regex(regexString);
            Match m = r.Match(remoteStr);
            if (m.Success)
            {
                matchVale = m.Value;
            }
            return matchVale;
        }
        #endregion

        #region 替换通过正则获取字符串所带的正则首尾匹配字符串
        /**********************************
         * 函数名称:GetHref
         * 功能说明:匹配页面的链接
         * 参    数:HtmlCode:html源代码
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string Url = @"http://www.baidu.com";
         *          string HtmlCode = o.GetRemoteHtmlCode(Url);
         *          string s = o.RegReplace(HtmlCode,"<title>","</title>");
         *          Response.Write(s);
         *          o.Dispose();
         * ********************************/
        /// <summary>
        /// 替换通过正则获取字符串所带的正则首尾匹配字符串
        /// </summary>
        /// <param name="regValue">要替换的值</param>
        /// <param name="regStart">正则匹配的首字符串</param>
        /// <param name="regEnd">正则匹配的尾字符串</param>
        /// <returns></returns>
        public static string RegReplace(string regValue, string regStart, string regEnd)
        {
            string s = regValue;
            if (!string.IsNullOrEmpty(regValue))
            {
                if (!string.IsNullOrEmpty(regStart))
                {
                    s = s.Replace(regStart, "");
                }
                if (!string.IsNullOrEmpty(regEnd))
                {
                    s = s.Replace(regEnd, "");
                }
            }
            return s;
        }
        #endregion

    }

}